/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dynaBlaster.view;

import java.util.HashMap;

import com.dynaBlaster.core.Point;
import com.dynaBlaster.game.Board;
import com.dynaBlaster.game.Game;
import com.dynaBlaster.game.GameConfig;
import com.dynaBlaster.game.Terrain;
import com.dynaBlaster.game.Tile;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * TileView: a View-variant designed for handling arrays of "icons" or other
 * drawables.
 * 
 */
public class BoardView extends View {

	// punkt w ktorym wyswietlic plansze na ekranie
	public Point offset;
	
	public Board board;

	// mapowanie terenu na reprezentacje graficzna
	public HashMap<Terrain, Bitmap> tileRepr;


	private final Paint mPaint = new Paint();

	public int screenTileSize;
	
	public BoardView(Context context, AttributeSet attrs, int defStyle, Board board) {
		super(context, attrs, defStyle);
		
		tileRepr = new HashMap<Terrain, Bitmap>();
		
		offset = new Point();
		
		// TypedArray a = context.obtainStyledAttributes(attrs,
		// R.styleable.TileView);
		//
		// mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);

		// TODO uzaleznic od wielkosci ekranu
		screenTileSize = GameConfig.TILE_SIZE;
		this.board = board;

		// a.recycle();
	}

	public BoardView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// TypedArray a = context.obtainStyledAttributes(attrs,
		// R.styleable.TileView);
		//
		// mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);

		
		// a.recycle();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// tutaj zmiana rozmiaru ekranu jest inputem dla gry oznaczajacym reset gry na nowym rozmiarze planszy
		
		screenTileSize = GameConfig.TILE_SIZE;
		
		int xTileCount = (int) Math.floor(w / screenTileSize);
		int yTileCount = (int) Math.floor(h / screenTileSize);

		offset = new Point(
			(w - (screenTileSize * xTileCount)) / 2, 
			(h - (screenTileSize * yTileCount)) / 2
		);

		board.reset(new Point(xTileCount, yTileCount));
	}

	/**
	 * Function to set the specified Drawable as the tile for a particular
	 * integer key.
	 * 
	 * @param key
	 * @param tile
	 */
	public void loadTile(Terrain key, Drawable tile) {
		Bitmap bitmap = Bitmap.createBitmap(screenTileSize, screenTileSize, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, screenTileSize, screenTileSize);
		tile.draw(canvas);

		tileRepr.put(key, bitmap);
	}

	

	public void destroyWall(Point p) {
		board.tileAt(p).terrain = Terrain.FREE;

	}

	


	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		// rysuje plansze
		for (int x = 0; x < board.size.x; ++x) {
			for (int y = 0; y < board.size.y; ++y) {
				Point index = new Point(x,y);
				Tile t = board.getTile(index);
				Point where = offset.add(index.mul(screenTileSize));
				canvas.drawBitmap(
					tileRepr.get(t.terrain), 
					where.x, 
					where.y, 
					mPaint
				);
			
			}
		}

	}


}
